<template>
  <div class="project-detail">
    <PageTemplate>
      <div slot="content">
         <el-form
          :model="projectInfo"
          label-position="right"
          label-width="150px"
          class="page-edit-colllapse-form"
          :rules="rules"
          ref="projectInfo">
          <el-row>
            <el-col :span="24">
              <el-form-item label="项目代码" prop="projectCode">
                  <el-input style="width:300px"  v-model="projectInfo.projectCode"></el-input>
              </el-form-item>
            </el-col>
          <el-col :span="24">
              <el-form-item label="项目名称" prop="projectName">
                <el-input style="width:300px"  v-model="projectInfo.projectName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="说明" prop="projectRemark">
                 <el-input style="width:600px" v-model="projectInfo.projectRemark" type="textarea" :rows="3"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-row class="row-empty-10"></el-row>
        <el-row>
           <el-button type="primary" @click="save">
            {{$t('cm.save')}}
          </el-button>
          <el-button type="primary" @click="back">
            {{$t('cm.return')}}
          </el-button>
        </el-row>
      </div>
    </PageTemplate>
  </div>
</template>

<script>
import { ALERT } from '@/utils/responseHandle'
import * as Constant from '@/utils/constant'
import PageTemplate from '@/components/common/PageTemplate'
import HasBtn from '@/mixins/HasBtn'
import { mapState, mapActions } from 'vuex'
import {COMMON} from '@/utils/commonUtil'
import opr_management from '@/api/opr_management'
export default {
  components: {
    PageTemplate, 
  },
  props: {
    // add：新增；edit：编辑
    type: {
      type: String,
      default: "add"
    },
  },
  data () {
    return {
      projectInfo: {
        projectCode:null,
        projectName:null,
        projectRemark:null,
      },
      rules: {
        projectCode: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        projectName: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
      },
    }
  },
  watch: {
    
  },
  computed: {
    
  },
  created () {
    if(this.type == "edit") this.getData();
    
  },
  methods: {
    ...mapActions('common', [
      'saveProjectItem',
    ]),
    getData(){
       opr_management.getProjectInfoById({
         id:this.$router.history.current.params.id
       }, result => {
        if (result.code === Constant.RT_SUCCESS) {
          this.projectInfo = result.data;
        } else {
          ALERT.error(result.msg)
        }
      })

    },
    //返回
    back(){
      this.$router.go(-1);//返回上一层
    },
    // 保存
    save(){
      this.$refs.projectInfo.validate((valid, error) => {
        if (valid) {
          const {
            projectCode, 
            projectName, 
            projectRemark,
          } = this.projectInfo;
          if(this.type == "edit"){
            opr_management.updateProjectInfoById({
              id:this.$router.history.current.params.id,
              projectCode,
              projectName, 
              projectRemark,
            }, result => {
              if (result.code === Constant.RT_SUCCESS) {
                this.back();
              } else {
                ALERT.error(result.msg)
              }
              
            })
        }else{
          opr_management.addProjectInfo({
              projectCode,
              projectName, 
              projectRemark,
            }, result => {
              if (result.code === Constant.RT_SUCCESS) {
                console.log('result', result)
                this.back();
                if(this.$route.query.type == "offerNumber"){
                  this.saveProjectItem(result.data);
                }
              } else {
                ALERT.error(result.msg)
              }
              
            })

        }
          
          

        }
      });
      

    },
   
  },
}
</script>

<style lang="scss">
.project-detail{
  .el-form{
    background: #fff;
    padding-top: 20px;
  }
}
</style>
